home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_dos.lha / dos / sphsrc.v08 / sph_falloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  2.8 KB  |  133 lines

  1. #include "HEADERS.h"
  2. /** FALLOC : falloc.c
  3. **/
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include "falloc.h"
  9. #include "fallocdefs.h"
  10.  
  11. /** FALLOCnew_chunk
  12. Effects: Allocates a new chunk.
  13. **/
  14.  
  15. FALLOCchunk *
  16. FALLOCnew_chunk()
  17. {
  18.   register FALLOCchunk *chunk;
  19.  
  20.   /* These are tiny, so we can just fatal error if they fail */
  21.  
  22.   MALLOC_FATAL(chunk, FALLOCchunk, 1, "new chunk");
  23.   MALLOC_FATAL(chunk->blocks, char *, 1, "first blockptr in new chunk");
  24.   MALLOC_FATAL(chunk->over_blocks, char *, 1, "first blockptr in new chunk");
  25.  
  26.   chunk->magic             = MAGIC;
  27.   chunk->free_bytes      = 0;
  28.   chunk->cur_block       = -1;
  29.   chunk->num_blocks      = 0;
  30.   chunk->num_over_blocks = 0;
  31.  
  32.   return chunk;
  33. }
  34.  
  35. /** FALLOCalloc
  36. Effects: Allocs from the given chunk.
  37. **/
  38.  
  39. char *
  40. FALLOCalloc(chunk, nbytes, zero)
  41.   register FALLOCchunk *chunk;
  42.   register int nbytes;
  43.   int zero;
  44. {
  45.   register char *ret;
  46.   register int  cb = chunk->cur_block;
  47.   register int  nb = chunk->num_blocks;
  48.   register int  ob = chunk->num_over_blocks;
  49.  
  50.   assert (chunk->magic == MAGIC);
  51.   
  52.   nbytes = (nbytes + ALIGN_SIZE) & ALIGN_MASK;
  53.  
  54.   if (nbytes <= chunk->free_bytes) {
  55.     ret            = chunk->cur_ptr;
  56.     chunk->cur_ptr    += nbytes;
  57.     chunk->free_bytes -= nbytes;
  58.   }
  59.  
  60.   else if (nbytes > FALLOC_BLOCK_SIZE) {
  61.     REALLOC_RET(chunk->over_blocks, char *, ob + 1, (char *) NULL);
  62.     MALLOC_RET(chunk->over_blocks[ob], char, nbytes, (char *) NULL);
  63.     ret = chunk->over_blocks[ob];
  64.     chunk->num_over_blocks++;
  65.   }
  66.  
  67.   else {
  68.     if (++chunk->cur_block >= chunk->num_blocks) {
  69.       REALLOC_RET(chunk->blocks, char *, nb + 1, (char *) NULL);
  70.       MALLOC_RET(chunk->blocks[nb], char, FALLOC_BLOCK_SIZE, (char *) NULL);
  71.       chunk->num_blocks++;
  72.     }
  73.  
  74.     chunk->free_bytes = FALLOC_BLOCK_SIZE - nbytes;
  75.     ret               = chunk->blocks[cb + 1];
  76.     chunk->cur_ptr    = ret + nbytes;
  77.   }
  78.  
  79. #ifdef THINK_C
  80.   if (zero) memset(ret, 0, nbytes);
  81. #else
  82.   if (zero) bzero(ret, nbytes);
  83. #endif
  84.   return ret;
  85. }
  86.  
  87. /** FALLOCfree
  88. Effects: Frees a falloc chunk.
  89. **/
  90.  
  91. void
  92. FALLOCfree(chunk)
  93.   register FALLOCchunk *chunk;
  94. {
  95.   register int i;
  96.  
  97.   assert (chunk->magic == MAGIC);
  98.  
  99.   chunk->magic = ~MAGIC;
  100.  
  101.   for (i = 0; i < chunk->num_blocks; i++) FREE(chunk->blocks[i]);
  102.   for (i = 0; i < chunk->num_over_blocks; i++) FREE(chunk->over_blocks[i]);
  103.  
  104.   FREE(chunk->blocks);
  105.   FREE(chunk->over_blocks);
  106.   FREE(chunk);
  107. }
  108.  
  109.  
  110. /** FALLOCclear_chunk
  111. Effects: Clears but does not free a chunk.
  112.          It becomes reusable.
  113. **/
  114.  
  115. void 
  116. FALLOCclear_chunk(chunk)
  117.   register FALLOCchunk *chunk;
  118. {
  119.   register int i;
  120.  
  121.   assert (chunk->magic == MAGIC);
  122.  
  123.   chunk->cur_block  = -1;
  124.   chunk->free_bytes = 0;
  125.  
  126.   for (i = 0; i < chunk->num_over_blocks; i++) FREE(chunk->over_blocks[i]);
  127.   chunk->num_over_blocks = 0;
  128.  
  129.   /* No need to free chunk->over_blocks, next realloc will take care of it */
  130. }
  131.  
  132.  
  133.